package com.coollang.smartbra.presenter;

import com.coollang.smartbra.fragment.ChangePassFragment;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.model.BaseModel;
import com.coollang.smartbra.model.LoginModel;
import com.coollang.smartbra.utils.Urls;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @Auther: Timmy
 * @Data: 2017/8/24
 * @Fuction:
 */

public class ChangePassFragmentPresenter {

    private ChangePassFragment mChangePassFragment;
    private final Gson mGson;

    public ChangePassFragmentPresenter(ChangePassFragment passFragment) {
        mChangePassFragment = passFragment;
        mGson = new Gson();
    }

    // 判断该手机号码是否注册
    public void checkUserIsRegister(String username) {

        MyHttp.getInstance().checkUserIsRegister(username, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                BaseModel model = mGson.fromJson(results, BaseModel.class);
                int code = Integer.parseInt(model.getCode());
                if (200 == code) {
                    // 获取成功
                    mChangePassFragment.onIsRegister(model.getData());
                } else {
                    mChangePassFragment.userIsRegister();
                    MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), Urls.CODE_Error_);
            }
        });


    }

    // 发送验证码
    public void sendSMSCode(String username) {
        MyHttp.getInstance().getCode(username, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (Urls.CODE_SUCCESS != code) {
                    mChangePassFragment.userIsRegister();
                    MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mChangePassFragment.userIsRegister();
                MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), Urls.CODE_Error_);
            }
        });
    }

    // 修改密码
    public void setChangePassInfo(String phone, String password, String code) {

        Map<String, String> params = new HashMap<>();
        params.put(Urls.USERNAME, phone);
        params.put(Urls.PASSWORD, password);
        params.put(Urls.VALIDATECODE, code);
        params.put(Urls.ZONE, "cn");

        MyHttp.getInstance().changePassword(params, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (code == 200) {
                    // 修改密码成功
                    mChangePassFragment.changePassSucceed();
                } else {
                    mChangePassFragment.userIsRegister();
                    MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), code);
                }

            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mChangePassFragment.userIsRegister();
                MyHttp.getInstance().showErrorToast(mChangePassFragment.getContext(), Urls.CODE_Error_);
            }
        });
    }
}
